/*
 * myradio.c
 *
 *  Created on: 2013.04.27.
 *      Author: Gábor
 */
#include "myradio.h"

//SEGMENT_VARIABLE(fixRadioPacket[RADIO_MAX_PACKET_LENGTH], U8, SEG_XDATA);
U8 fixRadioPacket[RADIO_MAX_PACKET_LENGTH];

bool ctsWentHigh = 0;

//radio_init() //shutdown lab fel, le, power up parancs
void radio_HW_powerUp(){
	  SI32_PBSTD_A_write_pins_high(SI32_PBSTD_2, 0x0002); //sdn to high
	  		   for(int i = 0;i<2000;i++);
	  		   SI32_PBSTD_A_write_pins_low(SI32_PBSTD_2, 0x0002); //sdn to low
	  		   for(int i = 0;i<2000;i++);
}


/*!
 * Waits for CTS to be high
 *
 * @return CTS value
 */
U8 PollCTS(void)
{
    return radio_comm_GetResp(0, 0);
}

U8 radio_comm_GetResp(U8 byteCount, U8* pData)
{
/*  SEGMENT_VARIABLE(ctsVal = 0u, U8, SEG_DATA);
  SEGMENT_VARIABLE(errCnt = RADIO_CTS_TIMEOUT, U16, SEG_DATA);*/
U8 ctsVal = 0;
U16 errCnt = RADIO_CTS_TIMEOUT;

  while (errCnt != 0)      //wait until radio IC is ready with the data
  {
	  ClearNsel();

    		//radio_hal_SpiWriteByte(0x44);    //read CMD buffer
/********************bugfix_1***************/
	   SI32_SPI_B_flush_rx_fifo(SI32_SPI_0);
	   SI32_SPI_B_flush_tx_fifo(SI32_SPI_0);


	   // Write a word to the slave, ignore data shifted in
	   SI32_SPI_B_clear_all_interrupts(SI32_SPI_0);
/**********************bugfix_1 END**********/

	SI32_SPI_B_write_tx_fifo_u8(SI32_SPI_1,0x44);
    		//ctsVal = radio_hal_SpiReadByte();
/********************bugfix**************
 *
 */
	   while(0 == SI32_SPI_B_is_shift_register_empty_interrupt_pending(SI32_SPI_1));
	   while (SI32_SPI_B_get_rx_fifo_count(SI32_SPI_1) < 1);

	   //ignore data that was shifted in during the tx process
	   SI32_SPI_B_flush_rx_fifo(SI32_SPI_1);
	   ctsVal = SI32_SPI_B_read_rx_fifo_u8(SI32_SPI_1);

	   // Read the response from the slave, shift out zeros
	   SI32_SPI_B_clear_all_interrupts(SI32_SPI_1);
	   SI32_SPI_B_write_tx_fifo_u8(SI32_SPI_1, 0x00);
	//   while(0 == SI32_SPI_B_is_shift_register_empty_interrupt_pending(SI32_SPI_0));
	   while (SI32_SPI_B_get_rx_fifo_count(SI32_SPI_1) < 1);

	   ctsVal = SI32_SPI_B_read_rx_fifo_u8(SI32_SPI_1);

/**********************bugfix END**********/

//idáig tart, hogy lekérdezzük CTS értéket
    if (ctsVal == 0xFF)
    {
      if (byteCount)
      {
        //radio_hal_SpiReadData(byteCount, pData);
    	  //helyette: ír nullát --> olvas    ciklusba (byteCount ciklushatár, pData:place to write..
    	  SpiReadData(byteCount, pData);
      }

      SetNsel();
      break;
    }
    SetNsel();
    errCnt--;
  }

  if( errCnt == 0 )
  {
    while(1)
    {
      /* ERROR!!!!  CTS should never take this long. */
    }
  }

  if (ctsVal == 0xFF)
  {
    ctsWentHigh = 1;
  }

  return ctsVal;
}


/*!
 * This function is used to load all properties and commands with a list of NULL terminated commands.
 * Before this function @si446x_reset should be called.
 */

U8 si446x_configuration_init(U8 numOfBytes, U8* pSetPropCmd)
{
 /* SEGMENT_VARIABLE(col, U8, SEG_DATA);
  SEGMENT_VARIABLE(numOfBytes, U8, SEG_DATA);*/

    if (radio_comm_SendCmdGetResp(numOfBytes, pSetPropCmd, 0, 0) != 0xFF)
    {
      /* Timeout occured */
      return SI446X_CTS_TIMEOUT;
    }

    //if (radio_hal_NirqLevel() == 0)
    if(!SI32_PBSTD_A_read_pin(SI32_PBSTD_2,0))
    {
      /* Get and clear all interrupts.  An error has occured...*/
      si446x_get_int_status(0, 0, 0);
      if (GET_INT_STATUS.CHIP_PEND & 0x08)
      {
        return SI446X_COMMAND_ERROR;
      }
    }

  return SI446X_SUCCESS;
}

U8 radio_comm_SendCmdGetResp(U8 cmdByteCount, U8* pCmdData, U8 respByteCount, U8* pRespData)
{
    radio_comm_SendCmd(cmdByteCount, pCmdData);
    return radio_comm_GetResp(respByteCount, pRespData);
}

/*!
 * Sends a command to the radio chip
 *
 * @param byteCount     Number of bytes in the command to send to the radio device
 * @param pData         Pointer to the command to send.
 */
void radio_comm_SendCmd(U8 byteCount, U8* pData)
{
    /* There was a bug in A1 hardware that will not handle 1 byte commands.
       It was supposedly fixed in B0 but the fix didn't make it at the last minute, so here we go again */
    if (byteCount == 1)
    	ctsWentHigh = 0;
       // byteCount++;

    while (!ctsWentHigh)
    {
        PollCTS();
    }
    ClearNsel();
    SpiWriteData(byteCount, pData);
    SetNsel();
    ctsWentHigh = 0;
}

void radio_comm_WriteData(U8 cmd, bool pollCts, U8 byteCount, U8* pData)
{
    if(pollCts)
    {
        while(!ctsWentHigh)
        {
            PollCTS();
        }
    }
    ClearNsel();
    //SpiWriteByte(cmd);
    SI32_SPI_B_write_tx_fifo_u8(SI32_SPI_1,cmd);
    SpiWriteData(byteCount, pData);
    SetNsel();
    ctsWentHigh = 0;
}

bool gRadio_CheckTransmitted(void)
{
  if (!SI32_PBSTD_A_read_pin(SI32_PBSTD_2,0))
  {
    /* Read ITs, clear pending ones */
    si446x_get_int_status(0, 0, 0);

    /* check the reason for the IT */
    if (GET_INT_STATUS.PH_PEND & 0x20)
    {
      return true;
    }
  }

  return false;
}

//
//void gRadio_CheckTransmitted(){
//		U8 getitstat[4] = {0x20,0x00,0x00,0x00};
//		U8* getitstatptr = &getitstat[0];
//		U8* itstatarrayptr;
//
//		var1 = radio_comm_SendCmdGetResp(4,getitstatptr,9,partarray_ptr);
//		if(partarray_ptr[5]&0x20)
//	    myUART0_send_char(var1); //FF-et küld ki elvileg
//	    //  myUART0_send_char(0x33);
//	    for(U8 k = 0;k<9;k++){
//	    myUART0_send_char(*partarray_ptr++); //végigszalad az előbb beolvasott pointeren
//	    }
//	    //lassito ciklus
//	    for(int i = 0;i<16000;i++);
//}
//BIT gRadio_CheckTransmitted(void)
//{
//  if (RF_NIRQ == FALSE)
//  {
//    /* Read ITs, clear pending ones */
//    si446x_get_int_status(0u, 0u, 0u);
//
//    /* check the reason for the IT */
//    if (Si446xCmd.GET_INT_STATUS.PH_PEND & SI446X_CMD_GET_INT_STATUS_REP_PACKET_SENT_PEND_BIT)
//    {
//      return TRUE;
//    }
//  }
//
//  return FALSE;
//}
